import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Dashboard from '../views/Dashboard.vue'
import UserManagement from '../views/UserManagement.vue'
import UserProfile from '../views/UserProfile.vue'  // 导入用户信息页面
import SystemSettings from '../views/Complaint/SystemSettings.vue'
import QueryPro from '../views/pro/QueryPro.vue'
import ComparePro from '../views/pro/ComparePro.vue'
import Enterprise from '../views/pro/Enterprise.vue'
import Collect from '../views/datamange/Collect.vue'
import QueryCollect from '../views/datamange/QueryCollect.vue'
import CorComts from '../views/Complaint/CorComts.vue'
import ProDetails from '../views/pro/ProDetails.vue'
import GuoChanDetail from '../views/GuoChanDetail.vue'
import ProDbDetail from '../views/pro/ProDbDetail.vue'
import ComDetails from '../views/pro/ComDetails.vue'
import CPcomplaint from '../views/Complaint/CPcomplaint.vue'
import QiYeComplaint from '../views/Complaint/QiYeComplaint.vue'
import CollectDetail from '../views/datamange/CollectDetail.vue'
Vue.use(Router)

// 创建路由实例
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        requiresAuth: false
      }
    },
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: {
        requiresAuth: true
      },
      // 移除默认重定向，让Home组件决定显示什么
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: Dashboard,
          meta: {
            title: '数智品诉定标平台',
            roles: ['admin'] // 移除guest角色，只允许企业用户和管理员访问
          }
        },
        {
          path: 'userManagement',
          name: 'UserManagement',
          component: UserManagement,
          meta: {
            title: '国家标准',
            roles: ['admin'] // 仅管理员可访问
          }
        },
        {
          path: 'guochanDetail',
          name: 'GuoChanDetail',
          component: GuoChanDetail,
          meta: {
            title: '标准详情',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'userProfile',
          name: 'UserProfile',
          component: UserProfile,
          meta: {
            title: '行业标准',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'queryPro',
          name: 'QueryPro',
          component: QueryPro,
          meta: {
            title: '产品查询',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'proDetails',
          name: 'ProDetails',
          component: ProDetails,
          meta: {
            title: '产品详情',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'proDbDetail',
          name: 'ProDbDetail',
          component: ProDbDetail,
          meta: {
            title: '产品对比详情',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'comparePro',
          name: 'ComparePro',
          component: ComparePro,
          meta: {
            title: '产品对比',
            roles: ['guest', 'enterprise', 'admin'] // 所有角色都可访问
          }
        },
        {
          path: 'enterprise',
          name: 'Enterprise',
          component: Enterprise,
          meta: {
            title: '企业信息',
            roles: ['enterprise', 'admin','guest'] // 企业和管理员可访问
          }
        },
        {
          path: 'comDetails',
          name: 'ComDetails',
          component: ComDetails,
          meta: {
            title: '企业详情',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'collect',
          name: 'Collect',
          component: Collect,
          meta: {
            title: '数据采集',
            roles: ['admin','enterprise'] // 仅管理员可访问
          }
        },
        {
          path: 'queryCollect',
          name: 'QueryCollect',
          component: QueryCollect,
          meta: {
            title: '数据查询',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'systemSettings',
          name: 'SystemSettings',
          component: SystemSettings,
          meta: {
            title: '产品投诉',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'CPcomplaint',
          name: 'CPcomplaint',
          component: CPcomplaint,
          meta: {
            title: '产品投诉',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'qiyeComplaint',
          name: 'QiYeComplaint',
          component: QiYeComplaint,
          meta: {
            title: '企业投诉详情',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'complaint',
          name: 'Complaint',
          component: CorComts,
          meta: {
            title: '企业投诉',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        },
        {
          path: 'collectDetail',
          name: 'CollectDetail',
          component: CollectDetail,
          meta: {
            title: '数据采集详情',
            roles: ['enterprise', 'admin'] // 企业和管理员可访问
          }
        }
      ]
    },
    {
      path: '*',
      redirect: '/login'
    }
  ]
})

// 路由拦截
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
  
  // 权限校验
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (token) {
      // 获取用户角色
      const userRole = localStorage.getItem('DzRoles')
      
      // 如果是子路由，检查是否有权限访问
      if (to.matched.length > 1 && to.matched[1].meta.roles) {
        const allowedRoles = to.matched[1].meta.roles
        
        // 检查用户角色是否在允许列表中
        if (allowedRoles.includes(userRole)) {
          next()
        } else {
          // 无权限访问，重定向到首页或显示提示
          Vue.prototype.$message.error('您没有权限访问该页面')
          next('/dashboard')
        }
      } else {
        // 父路由或没有设置角色限制，直接放行
        next()
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router